/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant;


import java.io.File;


/**
 * <p>
 * This class is an ant type that models a project dependency
 * on another project that can be built locally.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class LocalProject {

    private String group;
    private String name;
    private String antfile = "build.xml";
    private String target;
    private File dir;
    private LocalProjectBuilder builder = new DefaultLocalProjectBuilder();

    /**
     * Constructs a new <code>LocalProjectType</code>.
     */
    public LocalProject() {
    }


    /**
     * Returns the group that this local project belongs to.
     */
    public String getGroup() {
        return group;
    }

    /**
     * Sets the group that this local project belongs to.
     */
    public void setGroup(String group) {
        this.group = group;
    }

    /**
     * Returns the name of this local project.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of this local project.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the optional build file name of this local project. Defaults to
     * build.xml.
     */
    public String getAntfile() {
        return antfile;
    }

    /**
     * Sets the optional build file name of this local project.
     */
    public void setAntfile(String antfile) {
        this.antfile = antfile;
    }

    /**
     * Returns the optional target name which is called to build this local
     * project. This defaults to empty, which means the default target is used.
     */
    public String getTarget() {
        return target;
    }

    /**
     * Sets the optional target name which is called to build this local project.
     */
    public void setTarget(String target) {
        this.target = target;
    }

    /**
     * Returns the directory where this local project is located.
     */
    public File getDir() {
        return dir;
    }

    /**
     * Sets the directory where this local project is located.
     */
    public void setDir(File dir) {
        this.dir = dir;
    }

    /**
     * The LocalProjectBuilder that is used to build this local project. If this
     * is never set, it uses the DefaultLocalProjectBuilder.
     */
    public LocalProjectBuilder getBuilder() {
        return builder;
    }

    /**
     * Sets the LocalProjectBuilder to use to build this local project.
     */
    public void setBuilder(LocalProjectBuilder builder) {
        this.builder = builder;
    }

    /**
     * Returns a unique identifier for this project, which is a composite of the
     * group and name.
     *
     * @return  An id
     */
    public String getID() {
        return makeProjectID(group, name);
    }

    /**
     * Makes a unique project ID from the given group and name.
     *
     * @param   group The project's group
     * @param   name The project name
     * @return  The group, a hash symbol and then the project name
     */
    public static String makeProjectID(String group, String name) {
        return group + "#" + name;
    }

    /**
     * Validate that the project is configured correctly.
     *
     * @throws  SavantException If that validation fails.
     */
    public void validate() throws SavantException {
        if (group == null) {
            throw new SavantException("group attribute is required for a localproject");
        }

        if (name == null) {
            throw new SavantException("name attribute is required for a localproject");
        }

        if (dir == null) {
            throw new SavantException("dir attribute is required for a localproject");
        }
    }

    /**
     * Builds the local project that this types refers to using an ant task. If
     * the project doesn't exists, it is not built and this method returns.
     */
    public void build() throws SavantException {
        builder.build(this);
    }

    /**
     * Tests that this project is equal to the given object.
     *
     * @param   obj The Object to compare to this local project
     * @return  True if they are equal (both LocalProjects and the same local
     *          project), false otherwise
     */
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof LocalProject)) return false;

        final LocalProject localProjectType = (LocalProject) obj;

        if (!group.equals(localProjectType.group)) return false;
        if (!name.equals(localProjectType.name)) return false;

        return true;
    }

    /**
     * Returns the hash code of the local project using the group and name of the
     * local project as the hash values.
     *
     * @return  A unique hash code
     */
    public int hashCode() {
        int result;
        result = group.hashCode();
        result = 29 * result + name.hashCode();
        return result;
    }
}